var Student=require("../models/Student");
var Kecheng=require("../models/Kecheng");

// Kecheng.create({kid:100,name:"美术课"});
// Kecheng.create({kid:101,name:"音乐课"});
// Kecheng.create({kid:102,name:"体育课"});

exports.showindex=function(req,res){
    Student.find({},function(err,result){
        res.render("index",{
            "students":result
        })
    })
}

exports.showadd=function(req,res){
    //res.render("add")
    Kecheng.find({},function(err,result){
        res.render("add",{
            "quanbukecheng":result
        })
    })
}

exports.doadd=function(req,res){
    Student.create(req.query,function(){
        Kecheng.tianjiaxuesheng(req.query.Kechengs,req.query.sid,function(){
            res.send("插入成功")
        })
    })
}

exports.edit=function(req,res){
    var sid=req.params["sid"];
    Student.findOne({sid:sid},function(err,studentObj){
        Kecheng.find({},function(err,result){
            res.render("edit",{
                student:studentObj,
                "quanbukecheng":result
            })
        })
    })
}

exports.doedit=function(req,res){
    var sid=req.params.sid;
    let omitObj=req.query;
    Student.update({sid:sid},{$set:omitObj},{},function(err,result){
        if (!err) {
            res.send("修改成功")
        }
    })
}

exports.delete=function(req,res){
    var sid=req.params.sid;
    Student.remove({sid:sid},function(err,result){
        if (!err) {
            res.send("删除成功")
        }
    })
}

